package com.gisocn.onemap.presenter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.esri.android.map.Layer;
import com.esri.core.geodatabase.ShapefileFeatureTable;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.Feature;
import com.esri.core.map.FeatureResult;
import com.esri.core.tasks.query.QueryParameters;
import com.gisocn.onemap.base.BasePresenter;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.model.AppConfig;
import com.gisocn.onemap.model.AppVersion;
import com.gisocn.onemap.model.GetUserConfig;
import com.gisocn.onemap.model.GroupLayer;
import com.gisocn.onemap.model.ImgCLayer;
import com.gisocn.onemap.model.LayerInfoBean;
import com.gisocn.onemap.model.LayerQueryInfo;
import com.gisocn.onemap.model.Subcatalog;
import com.gisocn.onemap.model.VectorQuery;
import com.gisocn.onemap.net.ApiCallback;
import com.gisocn.onemap.utils.ExecutorsManager;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.utils.PullHelper;
import com.gisocn.onemap.utils.Utils;
import com.gisocn.onemap.view.MainView;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import jsqlite.Callback;
import jsqlite.Database;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.xmlpull.v1.XmlPullParserException;

/* loaded from: classes2.dex */
public class MainPresenter extends BasePresenter<MainView> {
    private String[] coldatas;
    private Context context;
    private ArrayList<Feature> featureList;
    private ArrayList<Geometry> geometries;
    private JsonObject landInfoObject;
    private ArrayList<JsonObject> list;
    private String targetDir;
    private AppConfig appConfig = new AppConfig();
    private ArrayList<VectorQuery> queryList = new ArrayList<>();
    private ArrayList<String> shpNames = new ArrayList<>();
    private ArrayList<String> dbNames = new ArrayList<>();
    private ArrayList<String> displayField = new ArrayList<>();
    private ArrayList<String> mapName = new ArrayList<>();
    private ArrayList<LayerQueryInfo> queryInfos = new ArrayList<>();

    public MainPresenter(MainView mainView, Context context) {
        this.context = context;
        attachView(mainView);
    }

    public void getAppConfigData() {
        InputStream fileInputStream;
        try {
            new File(Constant.SAVE_PATH);
            File file = new File(Constant.SAVE_PATH + Constant.APPCONFIG_XML_NAME);
            if (!file.exists()) {
                fileInputStream = this.context.getAssets().open(Constant.APPCONFIG_XML_NAME);
            } else {
                fileInputStream = new FileInputStream(file);
            }
            this.appConfig = (AppConfig) new Gson().fromJson(PullHelper.getAppConfig(fileInputStream), (Class<Object>) AppConfig.class);
            ((MainView) this.mvpView).getAppConfig(this.appConfig);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e2) {
            e2.printStackTrace();
        }
    }

    public void getAppConfigData(AppConfig appConfig) {
        this.appConfig = appConfig;
    }

    public void getAppConfigData(String str, String str2) {
        addSubscription(this.apiService.getUserConfig(str, str2), new ApiCallback<GetUserConfig>() { // from class: com.gisocn.onemap.presenter.MainPresenter.1
            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFailure(String str3) {
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFinish() {
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onSuccess(GetUserConfig getUserConfig) {
                if (getUserConfig.getReturnCode() == 0) {
                    File file = new File(Constant.SAVE_PATH + Constant.APPCONFIG_XML_NAME);
                    if (!file.exists()) {
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        file.delete();
                        try {
                            file.createNewFile();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                    }
                    byte[] bytes = getUserConfig.getMessage().getBytes();
                    try {
                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        try {
                            fileOutputStream.write(bytes);
                            fileOutputStream.close();
                        } catch (IOException e3) {
                            e3.printStackTrace();
                        }
                    } catch (FileNotFoundException e4) {
                        e4.printStackTrace();
                    }
                    ((MainView) MainPresenter.this.mvpView).getDataSuccess();
                    return;
                }
                ((MainView) MainPresenter.this.mvpView).getDataFail("获取在线配置文件失败," + getUserConfig.getMessage());
            }
        });
    }

    public void mapQuery(LinkedHashMap<String, Layer> linkedHashMap, Envelope envelope) {
        this.queryList.clear();
        this.shpNames.clear();
        this.displayField.clear();
        this.mapName.clear();
        this.targetDir = Constant.SAVE_PATH + "shp/";
        for (Map.Entry<String, Layer> entry : linkedHashMap.entrySet()) {
            if (!entry.getKey().equals("IMG_C") && !entry.getKey().equals("VEC_C")) {
                Iterator<Subcatalog> it = this.appConfig.getDynamicLayerList().getCatalog().getSubcatalog().iterator();
                while (it.hasNext()) {
                    Iterator<GroupLayer> it2 = it.next().getGroupLayers().iterator();
                    while (it2.hasNext()) {
                        Iterator<LayerInfoBean> it3 = it2.next().getLayer().iterator();
                        while (it3.hasNext()) {
                            LayerInfoBean next = it3.next();
                            if (!TextUtils.isEmpty(next.getShapeFile()) && next.getService().contains(entry.getKey())) {
                                this.shpNames.add(next.getShapeFile());
                                this.displayField.add(next.getDisplayField());
                                this.mapName.add(next.getName());
                            }
                        }
                    }
                }
            }
        }
        if (this.shpNames.size() > 0) {
            getShpData(this.shpNames, envelope, 0);
        }
    }

    public void sqlQuery(List<Layer> list, final Envelope envelope, final Point point, final int i, final SpatialReference spatialReference) {
        this.queryList.clear();
        this.displayField.clear();
        this.mapName.clear();
        this.dbNames.clear();
        for (Layer layer : list) {
            if (!layer.getName().equals("IMG_C") && !layer.getName().equals("VEC_C") && layer.isVisible()) {
                Iterator<Subcatalog> it = this.appConfig.getDynamicLayerList().getCatalog().getSubcatalog().iterator();
                while (it.hasNext()) {
                    Iterator<GroupLayer> it2 = it.next().getGroupLayers().iterator();
                    while (it2.hasNext()) {
                        Iterator<LayerInfoBean> it3 = it2.next().getLayer().iterator();
                        while (it3.hasNext()) {
                            LayerInfoBean next = it3.next();
                            if (!TextUtils.isEmpty(next.getShapeFile()) && (next.getName().equals(layer.getName()) || next.getService().equals(layer.getName()))) {
                                String shapeFile = next.getShapeFile();
                                String str = shapeFile.substring(0, shapeFile.indexOf(".")) + ".db";
                                File file = new File(Constant.SAVE_PATH_SHP + str);
                                if (!this.dbNames.contains(str) && file.exists()) {
                                    this.dbNames.add(str);
                                    this.displayField.add(next.getDisplayField());
                                    this.mapName.add(next.getName());
                                }
                            }
                        }
                    }
                }
            }
        }
        if (this.dbNames.size() > 0) {
            L.e("dbNames.size()" + this.dbNames.size());
            this.queryInfos.clear();
            ExecutorsManager.getInstance().execute(new Runnable() { // from class: com.gisocn.onemap.presenter.MainPresenter.2
                @Override // java.lang.Runnable
                public void run() {
                    MainPresenter mainPresenter = MainPresenter.this;
                    mainPresenter.sqlQuerySpace(mainPresenter.dbNames, envelope, point, 0, i, spatialReference);
                }
            });
            return;
        }
        ((MainView) this.mvpView).getTpkQueryFail("");
    }

    public void sqlQuerySpace(ArrayList<String> arrayList, final Envelope envelope, Point point, final int i, int i2, final SpatialReference spatialReference) {
        Log.e("sqlQuerySpace_dbNames", arrayList.get(i));
        Log.e("sqlQuerySpace", "sqlQuerySpace");
        LayerQueryInfo layerQueryInfo = new LayerQueryInfo();
        this.coldatas = null;
        this.geometries = new ArrayList<>();
        this.list = new ArrayList<>();
        String substring = arrayList.get(i).substring(arrayList.get(i).lastIndexOf("/") + 1);
        String substring2 = substring.substring(0, substring.lastIndexOf("."));
        String str = substring2 + ".xml";
        try {
            File file = new File(Constant.SAVE_PATH_SHP + arrayList.get(i));
            if (!file.exists()) {
                Log.e("sqlQuerySpace", "!file.exists()");
            }
            String file2 = file.toString();
            Class.forName("jsqlite.JDBCDriver").newInstance();
            Database database = new Database();
            database.open(file2, 1);
            Callback callback = new Callback() { // from class: com.gisocn.onemap.presenter.MainPresenter.3
                @Override // jsqlite.Callback
                public void types(String[] strArr) {
                }

                @Override // jsqlite.Callback
                public void columns(String[] strArr) {
                    MainPresenter.this.coldatas = strArr;
                }

                @Override // jsqlite.Callback
                public boolean newrow(String[] strArr) {
                    String pOLYGONWktToJson;
                    JsonParser jsonParser;
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("displayField", (String) MainPresenter.this.displayField.get(i));
                    for (int i3 = 0; i3 < strArr.length; i3++) {
                        if (!MainPresenter.this.coldatas[i3].equals("AsText(Geometry)")) {
                            jsonObject.addProperty(MainPresenter.this.coldatas[i3], strArr[i3]);
                        } else if (strArr[i3] != null) {
                            if (Constant.FILEPROVIDER_AUTHORITY.equals("com.gisocn.onemap.yt")) {
                                pOLYGONWktToJson = Utils.getMULTIPOLYGONWktToJson(strArr[i3], Constant.WKID);
                            } else if (strArr[i3].contains("), (")) {
                                pOLYGONWktToJson = Utils.getMULTIPOLYGONWktToJson(strArr[i3], Constant.WKID);
                            } else if (strArr[i3].contains("POINT(")) {
                                pOLYGONWktToJson = Utils.getPOINTWktToJson(strArr[i3], Constant.WKID);
                            } else if (strArr[i3].contains("MULTIPOLYGON")) {
                                pOLYGONWktToJson = Utils.getMULTIPOLYGONWktToJson(strArr[i3], Constant.WKID);
                            } else {
                                pOLYGONWktToJson = Utils.getPOLYGONWktToJson(strArr[i3], Constant.WKID);
                            }
                            try {
                                jsonParser = new JsonFactory().createJsonParser(pOLYGONWktToJson);
                            } catch (IOException e) {
                                e.printStackTrace();
                                jsonParser = null;
                            }
                            Geometry geometry = GeometryEngine.jsonToGeometry(jsonParser).getGeometry();
                            if (GeometryEngine.intersects(geometry, envelope, spatialReference)) {
                                MainPresenter.this.geometries.add(geometry);
                            } else {
                                jsonObject = null;
                            }
                        }
                    }
                    if (jsonObject != null) {
                        MainPresenter.this.list.add(jsonObject);
                    }
                    return false;
                }
            };
            String str2 = "select " + ("pk_uid," + this.displayField.get(i) + ",AsText(Geometry)") + " from " + substring2 + " where ROWID IN(SELECT pkid FROM idx_" + substring2 + "_Geometry WHERE pkid MATCH RTreeIntersects(" + envelope.getXMin() + "," + envelope.getYMin() + "," + envelope.getXMax() + "," + envelope.getYMax() + "))";
            Log.e("sql", str2);
            database.exec(str2, callback);
            database.close();
            int i3 = i + 1;
            if (this.list.size() > 0 && this.geometries.size() > 0) {
                layerQueryInfo.setLayerName(this.mapName.get(i));
                layerQueryInfo.setList(this.list);
                layerQueryInfo.setXmlName(str);
                layerQueryInfo.setGeometries(this.geometries);
                this.queryInfos.add(layerQueryInfo);
            }
            Log.e("query_index", "" + i3);
            if (i3 < arrayList.size()) {
                sqlQuerySpace(arrayList, envelope, point, i3, i2, spatialReference);
                return;
            }
            Log.e("query", "查询结束");
            ((MainView) this.mvpView).sqlQuery(this.queryInfos, point);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e2) {
            e2.printStackTrace();
        } catch (InstantiationException e3) {
            e3.printStackTrace();
        } catch (Exception e4) {
            e4.printStackTrace();
        }
    }

    public void sqlQueryLandInfo(final String str, String str2, final int i, final int i2, final int i3) {
        ExecutorsManager.getInstance().execute(new Runnable() { // from class: com.gisocn.onemap.presenter.MainPresenter.4
            @Override // java.lang.Runnable
            public void run() {
                MainPresenter.this.coldatas = null;
                String str3 = str;
                String substring = str3.substring(str3.lastIndexOf("/") + 1);
                String substring2 = substring.substring(0, substring.lastIndexOf("."));
                try {
                    String file = new File(str).toString();
                    Class.forName("jsqlite.JDBCDriver").newInstance();
                    Database database = new Database();
                    database.open(file, 1);
                    Callback callback = new Callback() { // from class: com.gisocn.onemap.presenter.MainPresenter.4.1
                        @Override // jsqlite.Callback
                        public void columns(String[] strArr) {
                            MainPresenter.this.coldatas = strArr;
                        }

                        @Override // jsqlite.Callback
                        public void types(String[] strArr) {
                            for (String str4 : strArr) {
                            }
                        }

                        @Override // jsqlite.Callback
                        public boolean newrow(String[] strArr) {
                            MainPresenter.this.landInfoObject = new JsonObject();
                            for (int i4 = 0; i4 < strArr.length; i4++) {
                                MainPresenter.this.landInfoObject.addProperty(MainPresenter.this.coldatas[i4], strArr[i4]);
                            }
                            return false;
                        }
                    };
                    String str4 = "SElECT * from " + substring2 + " where pk_uid=" + i;
                    Log.e("sql", str4);
                    database.exec(str4, callback);
                    database.close();
                    Log.e("query", "查询结束");
                    MainView mainView = (MainView) MainPresenter.this.mvpView;
                    JsonObject jsonObject = MainPresenter.this.landInfoObject;
                    int i4 = i;
                    mainView.sqlQueryLandInfo(jsonObject, i4, i2, i3, substring2, i4);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e2) {
                    e2.printStackTrace();
                } catch (InstantiationException e3) {
                    e3.printStackTrace();
                } catch (Exception e4) {
                    Log.e("Exception", "" + e4.getMessage());
                    e4.printStackTrace();
                }
            }
        });
    }

    void getShpData(final ArrayList<String> arrayList, final Envelope envelope, final int i) {
        try {
            ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(this.targetDir + arrayList.get(i));
            QueryParameters queryParameters = new QueryParameters();
            queryParameters.setGeometry(envelope);
            shapefileFeatureTable.queryFeatures(queryParameters, new CallbackListener<FeatureResult>() { // from class: com.gisocn.onemap.presenter.MainPresenter.5
                @Override // com.esri.core.map.CallbackListener
                public void onCallback(FeatureResult featureResult) {
                    int i2 = i;
                    MainPresenter.this.featureList = new ArrayList();
                    Iterator<Object> it = featureResult.iterator();
                    while (it.hasNext()) {
                        Object next = it.next();
                        if (next instanceof Feature) {
                            MainPresenter.this.featureList.add((Feature) next);
                        }
                    }
                    MainPresenter mainPresenter = MainPresenter.this;
                    mainPresenter.getQueryDta(mainPresenter.featureList, i2);
                    int i3 = i2 + 1;
                    if (i3 >= arrayList.size()) {
                        ((MainView) MainPresenter.this.mvpView).mapQuerySuccess(MainPresenter.this.queryList);
                        return;
                    }
                    MainPresenter mainPresenter2 = MainPresenter.this;
                    mainPresenter2.getShpData(mainPresenter2.shpNames, envelope, i3);
                }

                @Override // com.esri.core.map.CallbackListener
                public void onError(Throwable th) {
                    ((MainView) MainPresenter.this.mvpView).getDataFail("查询失败");
                }
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    void getQueryDta(ArrayList<Feature> arrayList, int i) {
        if (arrayList.size() > 0) {
            VectorQuery vectorQuery = new VectorQuery();
            vectorQuery.setFeatures(arrayList);
            vectorQuery.setMapName(this.mapName.get(i));
            vectorQuery.setShpName(this.shpNames.get(i));
            vectorQuery.setDisplayField(this.displayField.get(i));
            this.queryList.add(vectorQuery);
        }
    }

    public void getXmlInfo(String str, int i, int i2) {
        File file = new File(str);
        if (file.exists()) {
            try {
                try {
                    ((MainView) this.mvpView).getLandXmlSuccess(PullHelper.getQueryXML(new FileInputStream(file)), i, i2);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e2) {
                    e2.printStackTrace();
                }
                return;
            } catch (FileNotFoundException e3) {
                e3.printStackTrace();
                return;
            }
        }
        ((MainView) this.mvpView).getDataFail("没有相关文件");
    }

    /* JADX WARN: Code restructure failed: missing block: B:11:0x0037, code lost:
        r1 = r0.getName();
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public java.lang.String getDisplayField(java.lang.String r4, java.lang.String r5) {
        /*
            r3 = this;
            java.io.File r0 = new java.io.File
            r0.<init>(r4)
            boolean r4 = r0.exists()
            r1 = 0
            if (r4 == 0) goto L48
            java.io.FileInputStream r4 = new java.io.FileInputStream     // Catch: java.io.FileNotFoundException -> L43
            r4.<init>(r0)     // Catch: java.io.FileNotFoundException -> L43
            com.gisocn.onemap.model.QueryXML r4 = com.gisocn.onemap.utils.PullHelper.getQueryXML(r4)     // Catch: java.io.IOException -> L39 org.xmlpull.v1.XmlPullParserException -> L3e java.io.FileNotFoundException -> L43
            java.util.ArrayList r4 = r4.getFields()     // Catch: java.io.IOException -> L39 org.xmlpull.v1.XmlPullParserException -> L3e java.io.FileNotFoundException -> L43
            java.util.Iterator r4 = r4.iterator()     // Catch: java.io.IOException -> L39 org.xmlpull.v1.XmlPullParserException -> L3e java.io.FileNotFoundException -> L43
        L1d:
            boolean r0 = r4.hasNext()     // Catch: java.io.IOException -> L39 org.xmlpull.v1.XmlPullParserException -> L3e java.io.FileNotFoundException -> L43
            if (r0 == 0) goto L4f
            java.lang.Object r0 = r4.next()     // Catch: java.io.IOException -> L39 org.xmlpull.v1.XmlPullParserException -> L3e java.io.FileNotFoundException -> L43
            com.gisocn.onemap.model.QueryXML$Field r0 = (com.gisocn.onemap.model.QueryXML.Field) r0     // Catch: java.io.IOException -> L39 org.xmlpull.v1.XmlPullParserException -> L3e java.io.FileNotFoundException -> L43
            java.lang.String r2 = r0.getName()     // Catch: java.io.IOException -> L39 org.xmlpull.v1.XmlPullParserException -> L3e java.io.FileNotFoundException -> L43
            boolean r2 = r2.equals(r5)     // Catch: java.io.IOException -> L39 org.xmlpull.v1.XmlPullParserException -> L3e java.io.FileNotFoundException -> L43
            if (r2 == 0) goto L1d
            java.lang.String r4 = r0.getName()     // Catch: java.io.IOException -> L39 org.xmlpull.v1.XmlPullParserException -> L3e java.io.FileNotFoundException -> L43
            r1 = r4
            goto L4f
        L39:
            r4 = move-exception
            r4.printStackTrace()     // Catch: java.io.FileNotFoundException -> L43
            goto L4f
        L3e:
            r4 = move-exception
            r4.printStackTrace()     // Catch: java.io.FileNotFoundException -> L43
            goto L4f
        L43:
            r4 = move-exception
            r4.printStackTrace()
            goto L4f
        L48:
            java.lang.String r4 = "xmlPath"
            java.lang.String r5 = "没有相关配置文件"
            android.util.Log.e(r4, r5)
        L4f:
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: com.gisocn.onemap.presenter.MainPresenter.getDisplayField(java.lang.String, java.lang.String):java.lang.String");
    }

    public void getAppVersion(String str, Map<String, String> map) {
        addSubscription(this.apiService.getAppVersion(str, map), new ApiCallback<AppVersion>() { // from class: com.gisocn.onemap.presenter.MainPresenter.6
            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFinish() {
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onSuccess(AppVersion appVersion) {
                ((MainView) MainPresenter.this.mvpView).getAppVersionSuccess(appVersion);
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFailure(String str2) {
                ((MainView) MainPresenter.this.mvpView).getAppVersionFail(str2);
            }
        });
    }

    public ArrayList<ImgCLayer> initImgcSwitchData() {
        ArrayList<ImgCLayer> arrayList = new ArrayList<>();
        int i = 0;
        while (i < 10) {
            ImgCLayer imgCLayer = new ImgCLayer();
            LayerInfoBean layerInfoBean = new LayerInfoBean();
            StringBuilder sb = new StringBuilder();
            sb.append("2020年");
            i++;
            sb.append(i);
            sb.append("月影像");
            layerInfoBean.setName(sb.toString());
            imgCLayer.setLayer(layerInfoBean);
            arrayList.add(imgCLayer);
        }
        return arrayList;
    }
}
